package com.gxinyu.needle

import android.os.Bundle
import androidx.core.os.bundleOf
import com.gxinyu.needle.constans.*
import com.gxinyu.needle.databinding.FragmentMainBinding
import com.gxinyu.needle.login.LoginFragment

/**
 * @author gxinyu
 * @date  2022/5/8
 * @desc
 */
class MainFragment : BaseFragment<Nothing, FragmentMainBinding>(),
    BaseFragment.FragmentResultListener {

    override fun bindLayout(): FragmentMainBinding {
        return FragmentMainBinding.inflate(layoutInflater)
    }

    override fun bindListener() {
        super.bindListener()
        binding.btnGoLogin.setOnClickListener {
            //跳转到登录页面
            val bundle = bundleOf(
                LOGIN_NAME to "gxinyu",
                LOGIN_PASSWORD to "123456",
            )
            startForResult(LoginFragment(), REQUEST_KEY_LOGIN, bundle, this)
        }

        binding.btnGoDetail.setOnClickListener {
            //通过路由跳转到详情页
            start(RoutePath.LOGIN_DETAIL)
        }
    }

    override fun onFragmentResult(requestKey: String, result: Bundle) {
        when (requestKey) {
            REQUEST_KEY_LOGIN -> {
                val loginResult = result.getBoolean(LOGIN_RESULT)
                if (loginResult) {
                    binding.tvLoginState.text = "登录成功"
                } else {
                    binding.tvLoginState.text = "登录失败"
                }
            }
        }
    }
}

